<?php

/*
 * Copyright 2010 Mark Watkinson
 * 
 * This file is part of Luminous.
 * 
 * Luminous is free software: you can redistribute it and/or
 * modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Luminous is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Luminous.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */


 /**
   * \file luminous.php
   * \brief Easy (non-OO, single line call) API for Luminous.
   * \author Mark Watkinson
   * 
   * \defgroup LuminousEasyAPI LuminousEasyAPI
   * \example luminous_example.php Example calling and arrangement
   */

  
  require_once('luminous_luminous.class.php');
  require_once('luminous_cache.class.php');
  require_once('luminous_common_grammars.php');
  
  
  /**
   * \ingroup LuminousEasyAPI
   * \brief Maximum age of a cached file before it expires (in seconds), or -1
   */   
  global $LUMINOUS_MAX_AGE;
  $LUMINOUS_MAX_AGE = -1;
  
  /**
   * \ingroup LuminousEasyAPI
   * \brief Maximum lenth of time between cache purges (deletion of everything) in seconds, or -1
   */ 
  global $LUMINOUS_PURGE_TIME;
  $LUMINOUS_PURGE_TIME = -1;
  
  /**
   * \ingroup LuminousEasyAPI
   * \brief Simple call to luminous with language grammar
   * 
   * \param grammar The grammar to use to parse the string, as LuminousGrammar
   * \param src the source string to be formatted (string)
   * \param use_cache determines whether to use the caching system, 
   *    default is true. (true|false)
   * \return an HTML formatted piece of text representing the input string
   * \throw Exception if luminous encounters a fatal error (a more descriptive
   *    string will be set as its message)
   * \see Luminous
   * \see LuminousEasyAPI::$luminous_grammars
   */ 
  
  function luminous_grammar(LuminousGrammar $grammar, $src, $use_cache=true)
  {
    $md5 = md5($src);
    $o = false;
    $cache = null;
    
    if ($use_cache)
    {
      $cache = new LuminousCache($md5, $md5);
      $cache->cache_max_age = $GLOBALS['LUMINOUS_MAX_AGE'];
      $cache->purge_time = $GLOBALS['LUMINOUS_PURGE_TIME'];
      $cache->Purge();
      $o = $cache->ReadCache();
    }
    
    if ($o === false)
    {
      $l = new Luminous();      
      $o = $l->Easy_Parse($src, $grammar);      
      if ($use_cache)
        $cache->WriteCache($o);
    }
    return $o;
    
  }
  
  
  /**
   * \ingroup LuminousEasyAPI
   * \brief Simple call to luminous with language code
   * 
   * \param language The language code (which shall be looked up in 
   *    luminous_grammars. If no corresponding grammar is found, its default 
   *    will be used. In the event that no default is specified luminous, will 
   *    raise an Exception. (str)
   * \param src the source string to be formatted (string)
   * \param use_cache determines whether to use the caching system, 
   *    default is true. (true|false)
   * \return an HTML formatted piece of text representing the input string
   * \throw Exception if luminous encounters a fatal error (a more descriptive
   *    string will be set as its message)
   * \see Luminous
   * \see LuminousEasyAPI::$luminous_grammars
   */ 
  function luminous($language, $src, $use_cache=true)
  {
    global $luminous_grammars;
    $grammar = $luminous_grammars->GetGrammar($language);
    return luminous_grammar($grammar, $src, $use_cache);
  }
  
  
  /**
   * \ingroup LuminousEasyAPI
   * \brief Lookup table for language codes to grammars.
   * 
   * Global variable holding a LuminousGrammars language to grammar lookup 
   * table .This is used in any call to the easy API where a language code is 
   * specified. Add your own grammars to this using its AddGrammar method.
   * Anything stored in here is accessible by luminous()
   * \sa LuminousGrammars
   * \sa LuminousGrammars::AddGrammar
   */
  global $luminous_grammars;
  $luminous_grammars = new LuminousGrammars();
  
  
  $luminous_grammars->AddGrammar('as', 'LuminousGrammarActionscript');
  $luminous_grammars->AddGrammar(array('c', 'cpp', 'h', 'hpp', 'hxx', 'cxx'), 
    'LuminousGrammarCpp');
  $luminous_grammars->AddGrammar("css", 'LuminousGrammarCSS');
  $luminous_grammars->AddGrammar(array("cs", "csharp"), 'LuminousGrammarCSharp');
  $luminous_grammars->AddGrammar("html", 'LuminousGrammarJavaScriptEmbedded');
//   $luminous_grammars->AddGrammar("html", 'LuminousGrammarHTML');
  
  $luminous_grammars->AddGrammar(array('latex', 'tex'), 'LuminousGrammarLatex');
  
  $luminous_grammars->AddGrammar('mxml', 'LuminousGrammarActionscriptEmbedded');
  $luminous_grammars->AddGrammar(array('pas', 'pascal'), 'LuminousGrammarPascal');
  $luminous_grammars->AddGrammar("xml", 'LuminousGrammarHTML');
  
  $luminous_grammars->AddGrammar("java", 'LuminousGrammarJava');  
  $luminous_grammars->AddGrammar("js", 'LuminousGrammarJavaScript');
  $luminous_grammars->AddGrammar("php", 'LuminousGrammarPHP');
  $luminous_grammars->AddGrammar("py", 'LuminousGrammarPython');
  $luminous_grammars->AddGrammar("sh", 'LuminousGrammarBash');
  
  $luminous_grammars->AddGrammar("makefile", 'LuminousGrammarMakefile');
  
  $luminous_grammars->AddGrammar("diff", 'LuminousGrammarDiff');
  $luminous_grammars->AddGrammar("udiff", 'LuminousGrammarUDiff');
  $luminous_grammars->AddGrammar("sql", 'LuminousGrammarSQL');
  $luminous_grammars->AddGrammar("hs", 'LuminousGrammarHaskell');
  $luminous_grammars->AddGrammar("pl", 'LuminousGrammarPerl');
  $luminous_grammars->AddGrammar("m", 'LuminousGrammarMATLAB');
  $luminous_grammars->AddGrammar("vb", 'LuminousGrammarVisualBasic');
  $luminous_grammars->AddGrammar("rb", 'LuminousGrammarRuby');
  $luminous_grammars->AddGrammar("rhtml", 'LuminousGrammarRubyHTML');

  $luminous_grammars->SetDefaultGrammar('LuminousGrammarPlain');

?>